$(function(){

    $("#btnAdd").on('click', function () {
        showAddModal();
    });

    $("#typeName").on("focusout", function () {
        onInputValid("#typeName","#nameError");
    })
    $("#typeVal").on("focusout", function () {
        onInputValid("#typeVal","#valueError");
    })

    $("#dataTable").bootstrapTable({
        url: "/admin/college/ajaxQueryPage",
        contentType: "application/x-www-form-urlencoded", // 以form形式提交，方便获取参数
        method:"post",
        dataType: "json",
        pagination: true, //分页
        locale:"zh-CN",//表格汉化
        search: true, //显示搜索框
        sidePagination: "server", //服务端处理分页
        toolbar: '#toolbar',
        showRefresh: true, //显示刷新
        pageNumber:1,
        pageSize:10,
        queryParams: queryParams,
        responseHandler: responseHandler,
        columns: [
            {
                title: '#ID',
                field: 'collegesID',
                align: 'center',
                valign: 'middle',
                visible: false
            },
            {
                title: '名称',
                field: 'name',
                align: 'center',
                valign: 'middle'
            },
            {
                title: '成果数量',
                field: 'gain',
                align: 'center',
                valign: 'middle'
            },
            {
                title: '专家人数',
                field: 'experts',
                align: 'center',
                valign: 'middle'
            },
            {
                title: '机构属性',
                field: 'orgAttr',
                align: 'center',
                valign: 'middle'
            },
            {
                title: '研究领域',
                field: 'researchArea',
                align: 'center',
                valign: 'middle'
            },
            {
                title: '操作',
                field: 'id',
                align: 'center',
                events: 'actionEvents',
                formatter:function(value,row,index){
                    var e = '<a class="edit" href="javascript:;"><span class="glyphicon glyphicon-pencil btn-edit" /></a>&nbsp;&nbsp;&nbsp;&nbsp;';
                    var d = '<a class="remove" href="javascript:;" ><span class="glyphicon glyphicon-trash trash btn-trash"/></a> ';
                    return e+d;
                }
            }
        ]

    });
});

function queryParams(params) {
    return {
        pageSize: params.limit,

        pageNumber: params.offset,

        search: params.search
    };

}

function responseHandler(res) {

    if (res) {
        return {
            "rows": res[0],
            "total": res[1].total
        };

    } else {
        return {
            "rows": [],
            "total": 0
        };
    }

}

// 定义编辑按钮和删除的事件
window.actionEvents = {
    'click .edit': function (e, value, row, index) {
        showEditModal(row);
    },
    'click .remove': function (e, value, row, index) {
        $("#delForm").attr('action','/admin/college/ajaxDel');
        $("#delID").val(row.collegesID);
        $("#delConfirm").modal('show');
    }
};

function showEditModal(row){
    // 设置模态框标题
    var modalHead = '<h4 class="modal-title">高校信息修改</h4>';
    $("#modalHeader").html(modalHead);

    // 设置表单的内容
    $("#collegesID").val(row.collegesID);
    $("#name").val(row.name);
    $("#gain").val(row.gain);
    $("#experts").val(row.experts);
    $("#orgAttr").val(row.orgAttr);
    $("#researchArea").val(row.researchArea);

    $("#formID").attr('action','/admin/college/ajaxUpdate');
    $("#table-modal").modal('show');
}

function showAddModal(){
    // 设置模态框标题
    var modalHead = '<h4 class="modal-title">新增高校信息</h4>';
    $("#modalHeader").html(modalHead);

    $("#name").val('');
    $("#gain").val('');
    $("#experts").val('');
    $("#orgAttr").val('');
    $("#researchArea").val('');

    $("#formID").attr('action','/admin/college/ajaxAdd');
    $("#table-modal").modal('show');
}

// 验证
function onInputValid(selectId,errorId) {
    var val = $(selectId).val();

    if(val.trim().length < 1) {
        $(errorId).html("内容不能为空!").css({
            "color":"red",
            "font-family":"Arial",
            "font-size":"12px"
        });
        $(errorId).show();
        $(selectId).on('focusin',function(){
            $(errorId).hide();
        })
    }
}